﻿using System;
using System.Collections.Generic;
using System.IO;
using EnvDTE;
using Soat.SoPrism.TemplateWizards.Properties;

namespace Soat.SoPrism.TemplateWizards
{
    /// <summary>
    /// Cette classe représente un Wizard permettant de générer un projet template Module
    /// en proposant un Wizard permettant de saisir des informations supplémentaires
    /// </summary>
    public class ModuleProjectWizard : ProjectWizard
    {
        #region Consts

        /// <summary>
        /// Time pour le UnLock des fichiers par VS2010
        /// </summary>
        //const int MIN_TIME_FOR_PROJECT_TO_RELEASE_FILE_LOCK = 700;

        #endregion

        #region Private Members
        
        /// <summary>
        /// Répertoire d'origine du Projet Module
        /// </summary>
        private string originalFolder = string.Empty; 

        #endregion

        #region ProjectFinishedGenerating Method

        /// <summary>
        /// Runs custom wizard logic when a project item has finished generating.
        /// </summary>
        /// <param name="project">The project item that finished generating.</param>
        public override void ProjectFinishedGenerating(Project project)
        {
            try
            {
                var oldFilename = project.FullName;
                var strNewProjectFilename = string.Format(Resources.MODULE_PROJECT_NAME, this.WizardParameterHelper.GetParameterValue("$projectname$"), project.Name);

                var projectFileInfo = new FileInfo(project.FullName);
                var projectFolder = projectFileInfo.Directory;
                this.originalFolder = projectFolder.FullName;
                var newFolder = projectFolder.Parent.FullName + "\\" + string.Format(Resources.MODULE_PROJECT_FOLDER, this.WizardParameterHelper.GetParameterValue("$projectname$"), project.Name);

                // Supprime le projet venant d'être ajouté
                (this.WizardParameterHelper.Globals).DTE.Solution.Remove(project);

                System.Threading.Thread.Sleep(Resources.MIN_TIME_FOR_PROJECT_TO_RELEASE_FILE_LOCK.ToInt32());

                // Renome de fichier .csproj
                projectFileInfo.MoveTo(projectFolder.FullName + "\\" + strNewProjectFilename);

                // Déplace le contenu du répertoire du Projet crée
                projectFolder.MoveTo(newFolder);

                // Ajout à la solution du fichier Project renommé
                (this.WizardParameterHelper.Globals).DTE.Solution.AddFromFile(newFolder + "\\" + strNewProjectFilename, false);
            }
            catch (Exception)
            {
                // Nothing
            }

        }

        #endregion

        #region RunStarted Method

        /// <summary>
        /// Runs custom wizard logic at the beginning of a template wizard run.
        /// </summary>
        /// <param name="automationObject">The automation object being used by the template wizard.</param>
        /// <param name="replacementsDictionary">The list of standard parameters to be replaced.</param>
        /// <param name="runKind">A Microsoft.VisualStudio.TemplateWizard.WizardRunKind indicating the type of wizard run.</param>
        /// <param name="customParams">The custom parameters with which to perform parameter replacement in the project.</param>
        public override void RunStarted(object automationObject, Dictionary<string, string> replacementsDictionary, Microsoft.VisualStudio.TemplateWizard.WizardRunKind runKind, object[] customParams)
        {
            base.RunStarted(automationObject, replacementsDictionary, runKind, customParams);

            // Remplacement du répertoire de sortie
            //replacementsDictionary["$destinationdirectory$"] = string.Format(Resources.MODULE_PROJECT_NAME, replacementsDictionary["$destinationdirectory$"].Replace(replacementsDictionary["$dataname$"], "") + this.WizardParameterHelper.GetParameterValue("$projectname$"), replacementsDictionary["$dataname$"]).Replace(".csproj", "\\");
            replacementsDictionary["$destinationdirectory$"] = 
                string.Format(Resources.MODULE_PROJECT_NAME, 
                    replacementsDictionary["$destinationdirectory$"].Replace(replacementsDictionary["$dataname$"], "") + this.WizardParameterHelper.GetParameterValue("$projectname$"), 
                    replacementsDictionary["$dataname$"]).Replace(".csproj", "\\");
        }

        #endregion
        
        #region RunFinished Method

        /// <summary>
        /// Cette méthode permet de traiter la fin de l'éxécution du Wizard
        /// </summary>
        public override void RunFinished()
        {
            base.RunFinished();

            //// Suppression du répertoire d'origine s'il reste
            //try
            //{
            //    if (Directory.Exists(this.originalFolder))
            //    {
            //        Directory.Delete(this.originalFolder, true);
            //    }
            //}
            //catch (Exception)
            //{
            //    // Nothing
            //}
        }

        #endregion

    }
}
